package com.huijy.web.controller.management;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huijy.common.annotation.Log;
import com.huijy.common.core.controller.BaseController;
import com.huijy.common.core.domain.AjaxResult;
import com.huijy.common.enums.BusinessType;
import com.huijy.management.domain.Camera;
import com.huijy.management.service.ICameraService;
import com.huijy.common.utils.poi.ExcelUtil;
import com.huijy.common.core.page.TableDataInfo;

/**
 * 摄像头管理Controller
 *
 * @author luobo
 * @date 2021-10-03
 */
@RestController
@RequestMapping("/management/camera")
public class CameraController extends BaseController {
    @Autowired
    private ICameraService cameraService;

    /**
     * 查询摄像头管理列表
     */
    @PreAuthorize("@ss.hasPermi('management:camera:list')")
    @GetMapping("/list")
    public TableDataInfo list(Camera camera) {
        startPage();
        List<Camera> list = cameraService.selectCameraList(camera);
        return getDataTable(list);
    }

    /**
     * 导出摄像头管理列表
     */
    @PreAuthorize("@ss.hasPermi('management:camera:export')")
    @Log(title = "摄像头管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Camera camera) {
        List<Camera> list = cameraService.selectCameraList(camera);
        ExcelUtil<Camera> util = new ExcelUtil<Camera>(Camera.class);
        return util.exportExcel(list, "摄像头管理数据");
    }

    /**
     * 获取摄像头管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('management:camera:query')")
    @GetMapping(value = "/{cameraId}")
    public AjaxResult getInfo(@PathVariable("cameraId") Long cameraId) {
        return AjaxResult.success(cameraService.selectCameraByCameraId(cameraId));
    }

    /**
     * 新增摄像头管理
     */
    @PreAuthorize("@ss.hasPermi('management:camera:add')")
    @Log(title = "摄像头管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Camera camera) {
        return toAjax(cameraService.insertCamera(camera));
    }

    /**
     * 修改摄像头管理
     */
    @PreAuthorize("@ss.hasPermi('management:camera:edit')")
    @Log(title = "摄像头管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Camera camera) {
        return toAjax(cameraService.updateCamera(camera));
    }

    /**
     * 删除摄像头管理
     */
    @PreAuthorize("@ss.hasPermi('management:camera:remove')")
    @Log(title = "摄像头管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cameraIds}")
    public AjaxResult remove(@PathVariable Long[] cameraIds) {
        return toAjax(cameraService.deleteCameraByCameraIds(cameraIds));
    }

    /**
     * 检查摄像头在线状态
     */
    @PreAuthorize("@ss.hasPermi('management:camera:online')")
    @GetMapping("/checkOnline/{cameraId}")
    public AjaxResult checkOnline(@PathVariable Long cameraId) {
        boolean b = cameraService.checkCameraIsOnline(cameraId);
        if (b) {
            return AjaxResult.success("设备在线");
        } else {
            return AjaxResult.success("设备离线");
        }
    }

}
